package com.service.impl;

import com.dao.GoodsDao;
import com.dao.impl.GoodsDaoImpl;
import com.pojo.PageBean;
import com.pojo.TbGoods;
import com.pojo.TbGoodsKz;
import com.service.GoodsService;

import java.sql.SQLException;
import java.util.List;

public class GoodsServiceImpl implements GoodsService {

    private GoodsDao goodsDao=new GoodsDaoImpl();

    @Override
    public void addGoods(TbGoodsKz tbGoodsKz) throws SQLException {
        //先校验数据，判断为不为空

        //将数据添加到快照表，得到一个快照id
        long kzid=goodsDao.addGoodsKz(tbGoodsKz);
        long itemid = goodsDao.addGoods(kzid);

        System.err.println("快照 id===>" + kzid + "-- 商品 id=====>"+itemid);
    }

    @Override
    public PageBean<TbGoods> getGoodsPageByTypeId(int typeId, int currentPage) {
        PageBean<TbGoods>pageBean=new PageBean<>();
        //查询所有的数量
        int goodsCountByTypeId = goodsDao.getGoodsCountByTypeId(typeId);
        //先算出来当前应该加载的数据是哪些先算出来当前应该加载的数据是哪些,然后进行查询 sql
        int pageSize=pageBean.getPageSize();//每页的数量
        List<TbGoods> goodsList = goodsDao.getGoodsByTypeIdAndLimit(typeId, (currentPage - 1) * pageSize, pageSize);
        pageBean.setTotalPage(goodsCountByTypeId);//设置总条数
        pageBean.setList(goodsList);//设置查询到的数据
        System.err.println(goodsCountByTypeId);
        return pageBean;
    }

    @Override
    public TbGoods getGoodById(int id) {
        if(id<=0){
            //id不存在，不查询
            return null;

        }
        return goodsDao.getGoodById(id);
    }
}
